package com.aistar.service.impl;

import com.aistar.mapper.QuestionSetMapper;
import com.aistar.pojo.QuestionSet;
import com.aistar.pojo.QuestionSetExample;
import com.aistar.service.QuestionSetService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionSetServiceImpl implements QuestionSetService {
    @Autowired
    private QuestionSetMapper questionSetMapper;

    @Override
    public ServerResponse add(QuestionSet questionSet) {
        questionSet.setQuestionSetCreateTime(new Date());
        int rows = questionSetMapper.insertSelective(questionSet);
        if (rows > 0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse getLatest(Integer custId) {
        QuestionSetExample example = new QuestionSetExample();

        QuestionSetExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerIdEqualTo(custId);

        Page<QuestionSet> pageInfo = PageHelper.startPage(1, 1);
        System.out.println("pageInfo里面信息：" + pageInfo);
        example.setOrderByClause("question_set_create_time desc");
        List<QuestionSet> questionSetList = questionSetMapper.selectByExample(example);

        if (questionSetList != null && questionSetList.size() > 0)
            return ServerResponse.getSuccess(questionSetList.get(0));
        return ServerResponse.getFailed();

    }

    @Override
    public ServerResponse getById(Integer questionSetId) {
        QuestionSet questionSet = questionSetMapper.selectByPrimaryKey(questionSetId);
        if (questionSet != null)
            return ServerResponse.getSuccess(questionSet);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse modify(QuestionSet questionSet) {
        int rows = questionSetMapper.updateByPrimaryKeySelective(questionSet);
        if (rows > 0)
            return ServerResponse.modifySuccess();
        return ServerResponse.modifyFailed();
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        QuestionSetExample example = new QuestionSetExample();
        example.createCriteria()
            .andCustomerIdEqualTo(customerId)
            .andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        example.setOrderByClause("question_set_create_time desc");
        List<QuestionSet> questionSetList = questionSetMapper.selectByExample(example);
        if (questionSetList != null && questionSetList.size() > 0)
            return ServerResponse.getSuccess(questionSetList);
        return ServerResponse.getFailed();
    }

}
